define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'depositrefund/index',
                    add_url: 'depositrefund/add',
                    edit_url: 'depositrefund/edit',
                    del_url: 'depositrefund/del',
                    multi_url: 'depositrefund/multi',
                    table: 'deposit_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'merid', title: __('Merid')},
                        {field: 'order_id', title: __('Order_id')},
                        {field: 'order.order_amount', title: __('押金金额')},
                        {field: 'user.username', title: __('User_id'), operate: 'LIKE %...%'},
                        {field: 'order_goods.goods_name', title: __('Order_goods_id'), operate: 'LIKE %...%'},
                        {field: 'shipping.name', title: __('Shipping_id'), operate: 'LIKE %...%'},
                        {field: 'shipping_no', title: __('Shipping_no'), operate: 'LIKE %...%'},
                        {field: 'status', title: __('Status'), searchList: Config.statusList, visible: false},
                        {field: 'status_text', title: __('Status'), operate: false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'user_remark', title: __('User_remark')},
                        {field: 'admin_remark', title: __('Admin_remark')},
                        {
                            field: 'operate', 
                            title: __('Operate'), 
                            buttons: [
                                {
                                    name: 'pass',
                                    text: '审核通过', 
                                    title: '审核通过', 
                                    icon: '', 
                                    classname: 'btn btn-xs btn-danger btn-ajax', 
                                    confirm: '审核通过将把押金退回到用户余额，确认进行此操作吗？', 
                                    url: 'depositrefund/pass',
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh', {});
                                    }
                                },
                                {
                                    name: 'deny',
                                    text: '驳回',
                                    title: '驳回',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    url: 'depositrefund/deny',
                                    success: function(data, ret) {
                                        table.bootstrapTable('refresh',{});
                                    }
                                }
                            ],
                            table: table, 
                            events: Table.api.events.operate, 
                            formatter: function(value, row, index) {
                                
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data('operate-del', null);
                                $(table).data('operate-edit', null);
                                if(row.status != 0) {
                                    $(table).data('operate-pass', null);
                                    $(table).data('operate-deny', null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deny: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});